package com.taokatao.cardmee.common.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.taokatao.cardmee.R;
import com.umeng.socialize.bean.SHARE_MEDIA;


public class ShareDialog extends Dialog {

    public ShareDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public interface OnShareClickListener {
        void onPlatformClick(Dialog dialog, SHARE_MEDIA platform);
    }

    public static class Builder {
        private final Context context;
        private final boolean shadow = true;
        private final boolean canCancel = true;
        private OnShareClickListener callback;



        public Builder(Context context) {
            this.context = context;
        }

        public Builder setCallback(OnShareClickListener callback) {
            this.callback = callback;
            return this;
        }

        public ShareDialog create() {
            final ShareDialog dialog = new ShareDialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);

            View view = LayoutInflater.from(context).inflate(R.layout.dialog_common_share, null);

            final View.OnClickListener c = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SHARE_MEDIA platform = null;
                    int i = view.getId();
                    if (i == R.id.lay_wechat) {
                        platform = SHARE_MEDIA.WEIXIN;
                    } else if (i == R.id.lay_moments) {
                        platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                    } else if (i == R.id.lay_qq) {
                        platform = SHARE_MEDIA.QQ;
                    } else if (i == R.id.lay_qzone) {
                        platform = SHARE_MEDIA.QZONE;
                    } else if (i == R.id.lay_weibo) {
                        platform = SHARE_MEDIA.SINA;
                    }

                    if (callback != null) callback.onPlatformClick(dialog ,platform);
                }
            };

            view.findViewById(R.id.lay_wechat).setOnClickListener(c);
            view.findViewById(R.id.lay_moments).setOnClickListener(c);
            view.findViewById(R.id.lay_qq).setOnClickListener(c);
            view.findViewById(R.id.lay_qzone).setOnClickListener(c);
            view.findViewById(R.id.lay_weibo).setOnClickListener(c);

            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setGravity(Gravity.BOTTOM);
            win.setWindowAnimations(R.style.Animation_Bottom_Rising);

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);

            return dialog;
        }
    }
}
